package chapter3;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author ycj
 * @version 1.0
 * @description: TODO
 * @date 2022/5/17
 */
public class Source3_3 {
    private static int data=0;
    private static final ReentrantLock   reentrantLock=new ReentrantLock();

    public static void main(String[] args) throws InterruptedException {
        new Thread(()->{
            if(reentrantLock.tryLock()){
                try {
                    System.out.println("线程:"+Thread.currentThread().getName()+"获取到显示锁");
                    Thread.sleep(2000000000);
                    data++;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    reentrantLock.unlock();
                }
            }else{
                System.out.println("线程:"+Thread.currentThread().getName()+"没有获取到显示锁");
            }
        }).start();

        Thread.sleep(1000);
        reentrantLock.lock();
        try{
            System.out.println("data="+data);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            reentrantLock.unlock();
        }

    }
}
